package view;

import Protected.Protected;
import controller.*;
import model.Player;


import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Server {
    private static Connection cn;
    private static int total;
    static class ServerThread extends Thread {

        Socket socket;
        public ServerThread(Socket socket) {
            this.socket=socket;
        }


        @Override
        public void run() {
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                Player player=null;
                String id;
                out.write("欢迎来到架空界。");
                out.newLine();
                out.flush();
                while (player==null) {
                    out.write("请输入您的角色id，如需注册请输入register");
                    out.newLine();
                    out.flush();
                    if(Objects.equals(id = in.readLine(), "register")){
                        boolean sign=false;
                        while(!sign){
                            out.write("请输入新建角色名称");
                            out.newLine();
                            out.flush();
                            String name = in.readLine();
                            out.write("请输入密码");
                            out.newLine();
                            out.flush();
                            String pwd = in.readLine();
                            System.out.println(1);
                            int num= DatabaseController.getTotal();
                            num=10000+num;
                            id=String.valueOf(num);
                            player=new Player(0,10,10,10,10,100,100,100,100,100,100,id,name,
                                    Protected.defaultlocation,1);
                            sign=DatabaseController.addnewPlayer(player,pwd);
                            if(!sign){
                                out.write("已存在该名称！");
                                out.newLine();
                                out.flush();
                            }
                        }
                        out.write("您的id为:"+id);
                        out.newLine();
                        out.flush();
                    }else {
                            out.write("请输入密码");
                            out.newLine();
                            out.flush();
                            String pwd = in.readLine();
                            player=DatabaseController.check(id,pwd,out);
                    }
                }

                MessageController.addPlayerChannels(player.getId(),out);
                PlayerController.initPlayer(player);
                for(;;){
                    UserInput.dealInput(player,in.readLine());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static int PORT_NUM = 1888;



    static public void main(String[] args) throws IOException {

        try{
            Class.forName( "com.mysql.cj.jdbc.Driver" );
            cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mud", Protected.sUsr, Protected.sPwd );
            DatabaseController.setConnection(cn);
        }catch (Exception e){
            e.printStackTrace();
        }

        RoomController.createRooms();
        ItemController.listItems();

        ServerSocket serverSocket = new ServerSocket(PORT_NUM);
        Runnable Controltask=new Runnable() {
            public void run() {
                try {
                    RoomController.npcMove();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        Runnable monsterTask=new Runnable() {
            public void run() {
                try {
                    RoomController.monsterFlush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        service.scheduleAtFixedRate(Controltask,0,10, TimeUnit.SECONDS);
        service.scheduleAtFixedRate(monsterTask,0,60, TimeUnit.SECONDS);
        for (;;) {
            Socket socket = serverSocket.accept();
            new ServerThread(socket).start();

        }
    }//end main

}//end Server class
